import {createRouter, createWebHashHistory} from "vue-router";
import Home from "../views/Home.vue";

const routes = [
    {
        path: '/',
        redirect: '/homepage'
    }, {
        path: "/",
        name: "Home",
        component: Home,
        children: [
            {
                path: "/dashboard",
                name: "dashboard",
                meta: {
                    title: '系统首页'
                },
                component: () => import ( /* webpackChunkName: "dashboard" */ "../views/Dashboard.vue")
            }, {
                path: "/table",
                name: "basetable",
                meta: {
                    title: '表格'
                },
                component: () => import ( /* webpackChunkName: "table" */ "../views/BaseTable.vue")
            }, {
                path: "/charts",
                name: "basecharts",
                meta: {
                    title: '图表'
                },
                component: () => import ( /* webpackChunkName: "charts" */ "../views/BaseCharts.vue")
            }, {
                path: "/form",
                name: "baseform",
                meta: {
                    title: '表单'
                },
                component: () => import ( /* webpackChunkName: "form" */ "../views/BaseForm.vue")
            }, {
                path: "/tabs",
                name: "tabs",
                meta: {
                    title: 'tab标签'
                },
                component: () => import( /* webpackChunkName: "form" */ "../views/Tabs.vue")
            }, {
                path: "/DoctorList",
                name: "DoctorList",
                meta: {
                    title: '医生管理'
                },
                component: () => import ( /* webpackChunkName: "DoctorList" */ "../views/Manager/DoctorList.vue")
            }, {
                path: "/permission",
                name: "permission",
                meta: {
                    title: '权限管理',
                    permission: true
                },
                component: () => import ( /* webpackChunkName: "permission" */ "../views/Permission.vue")
            }, {
                path: "/i18n",
                name: "i18n",
                meta: {
                    title: '国际化语言'
                },
                component: () => import ( /* webpackChunkName: "i18n" */ "../views/I18n.vue")
            }, {
                path: "/upload",
                name: "upload",
                meta: {
                    title: '上传插件'
                },
                component: () => import ( /* webpackChunkName: "upload" */ "../views/Upload.vue")
            }, {
                path: "/icon",
                name: "icon",
                meta: {
                    title: '自定义图标'
                },
                component: () => import ( /* webpackChunkName: "icon" */ "../views/Icon.vue")
            }, {
                path: '/404',
                name: '404',
                meta: {
                    title: '找不到页面'
                },
                component: () => import (/* webpackChunkName: "404" */ '../views/404.vue')
            }, {
                path: '/403',
                name: '403',
                meta: {
                    title: '没有权限'
                },
                component: () => import (/* webpackChunkName: "403" */ '../views/403.vue')
            }, {
                path: '/user',
                name: 'User',
                meta: {
                    title: '个人中心'
                },
                component: () => import (/* webpackChunkName: "user" */ '../views/User.vue')
            }, {
                path: '/chooseDepartment',
                name: 'ChooseDepartment',
                meta: {
                    title: '科室选择'
                },

                component: () => import (/* webpackChunkName:Editor "editor" */ '../views/Patient/ChooseDepartment.vue')
            }, {
                path: '/RchooseDepartment',
                name: 'RChooseDepartment',
                meta: {
                    title: '科室选择'
                },

                component: () => import (/* webpackChunkName: "editor" */ '../views/Reception/RChooseDepartment.vue')
            },{
                path: '/medicinelist',
                name: 'MedicineList',
                meta: {
                    title: '药单'
                },
                component: () => import (/* webpackChunkName: "editor" */ '../views/Patient/MedicineList.vue')
            }, {
                path: '/chooseDoctor',
                name: 'ChooseDoctor',
                meta: {
                    title: '挂号'
                },
                component: () => import (/* webpackChunkName: "editor" */ '../views/Patient/ChooseDoctor.vue')
            },
            {
                path: '/RchooseDoctor',
                name: 'RChooseDoctor',
                meta: {
                    title: '线下挂号'
                },
                component: () => import (/* webpackChunkName: "editor" */ '../views/Reception/RChooseDoctor.vue')
            },
            {
                path: '/QRCode',
                name: 'QRCode',
                meta: {
                    title: '挂号缴费'
                },
                component: () => import (/* webpackChunkName: "editor" */ '../views/Patient/QRCode.vue')
            }, {
                path: '/personalCase',
                name: 'PersonalCase',
                meta: {
                    title: '个人病历'
                },
                component: () => import (/* webpackChunkName: "editor" */ '../views/Patient/PersonalCase.vue')
            }, {
                path: '/patientPay',
                name: 'PatientPay',
                meta: {
                    title: '药单缴费'
                },
                component: () => import (/* webpackChunkName: "editor" */ '../views/Patient/PatientPay.vue')
            }, {
                path: '/orderList',
                name: 'OrderList',
                meta: {
                    title: '挂号记录'
                },
                component: () => import (/* webpackChunkName: "editor" */ '../views/Patient/OrderList.vue')
            }, {
                path: '/patientList',
                name: 'PatientList',
                meta: {
                    title: '挂号列表'
                },
                component: () => import (/* webpackChunkName: "editor" */ '../views/Doctor/PatientList.vue')
            }, {
                path: '/patientInf',
                name: 'PatientInf',
                meta: {
                    title: '患者信息'
                },
                component: () => import (/* webpackChunkName: "editor" */ '../views/Doctor/PatientInf.vue')
            }, {
                path: '/dpatientCase',
                name: 'DPatientCase',
                meta: {
                    title: '患者病历'
                },
                component: () => import (/* webpackChunkName: "editor" */ '../views/Doctor/PatientCase.vue')
            },{
                path: '/patientCase',
                name: 'PatientCase',
                meta: {
                    title: '病历打印'
                },
                component: () => import (/* webpackChunkName: "editor" */ '../views/Reception/PatientCase.vue')
            }
            ,
            {
                path: '/prescribe',
                name: 'Prescribe',
                meta: {
                    title: '药库  '
                },
                component: () => import (/* webpackChunkName: "editor" */ '../views/Doctor/Prescribe.vue')
            },
            {
                path: '/GetQueue',
                name: 'GetQueue',
                meta: {
                    title: '线下挂号'
                },
                component: () => import (/* webpackChunkName: "editor" */ '../views/Reception/GetQueue.vue')
            },
            {
                path: '/GetDate',
                name: 'GetDate',
                meta: {
                    title: '药单日期'
                },
                component: () => import (/* webpackChunkName: "editor" */ '../views/Reception/GetDate.vue')
            },
            {
                path: '/RMedicineList',
                name: 'RMedicineList',
                meta: {
                    title: '药单'
                },
                component: () => import (/* webpackChunkName: "editor" */ '../views/Reception/RMedicineList.vue')
            },
            {
                path: '/PayList',
                name: 'PayList',
                meta: {
                    title: '缴费列表'
                },
                component: () => import (/* webpackChunkName: "editor" */ '../views/Reception/PayList.vue')
            },
            {
                path: '/patientCaseList',
                name: 'PatientCaseList',
                meta: {
                    title: '患者列表'
                },
                component: () => import (/* webpackChunkName: "editor" */ '../views/Reception/PatientCaseList.vue')
            },

            {
                path: '/drugManagement',
                name: 'DrugManagement',
                meta: {
                    title: '药库管理'
                },
                component: () => import (/* webpackChunkName: "editor" */ '../views/Manager/DrugManagement.vue')
            },
            {
                path: '/drugInput',
                name: 'DrugInput',
                meta: {
                    title: '药品入库'
                },
                component: () => import (/* webpackChunkName: "editor" */ '../views/Manager/DrugInput.vue')
            },{
                path: '/reception',
                name: 'Reception',
                meta: {
                    title: '前台列表'
                },
                component: () => import (/* webpackChunkName: "editor" */ '../views/Manager/ReceptionList.vue')
            },{
                path: '/MPatientList',
                name: 'MPatientList',
                meta: {
                    title: '用户列表'
                },
                component: () => import (/* webpackChunkName: "editor" */ '../views/Manager/MPatientList.vue')
            },{
                path: '/ManageDepartment',
                name: 'ManageDepartment',
                meta: {
                    title: '科室管理'
                },
                component: () => import (/* webpackChunkName: "editor" */ '../views/Manager/ManageDepartment.vue')
            },{
                path: '/Scheduling',
                name: 'Scheduling',
                meta: {
                    title: '排班'
                },
                component: () => import (/* webpackChunkName: "editor" */ '../views/Manager/Scheduling.vue')
            }
            ,{
                path: '/patientuser',
                name: 'PatientUser',
                meta: {
                    title: '个人信息'
                },
                component: () => import (/* webpackChunkName: "editor" */ '../views/Patient/PatientUser.vue')
            }
            ,{
                path: '/receptionuser',
                name: 'ReceptionUser',
                meta: {
                    title: '个人信息'
                },
                component: () => import (/* webpackChunkName: "editor" */ '../views/Reception/ReceptionUser.vue')
            },{
                path: '/manageruser',
                name: 'ManagerUser',
                meta: {
                    title: '个人信息'
                },
                component: () => import (/* webpackChunkName: "editor" */ '../views/Manager/ManagerUser.vue')
            },{
                path: '/RChangePassword',
                name: 'RChangePassword',
                meta: {
                    title: '修改密码'
                },
                component: () => import (/* webpackChunkName: "editor" */ '../views/Reception/RChangePassword.vue')
            },{
                path: '/PChangePassword',
                name: 'PChangePassword',
                meta: {
                    title: '修改密码'
                },
                component: () => import (/* webpackChunkName: "editor" */ '../views/Patient/PChangePassword.vue')
            },{
                path: '/DChangePassword',
                name: 'DhangePassword',
                meta: {
                    title: '修改密码'
                },
                component: () => import (/* webpackChunkName: "editor" */ '../views/Doctor/DChangePassword.vue')
            },{
                path: '/StandingBook',
                name: 'StandingBook',
                meta: {
                    title: '台账管理'
                },
                component: () => import (/* webpackChunkName: "editor" */ '../views/Manager/StandingBook.vue')
            },

            {
                path: '/News_Manager',
                name: 'News_Manager',
                meta: {
                    title: '新闻管理'
                },
                component: () => import (/* webpackChunkName: "editor" */ '../views/Manager/News_Manager.vue')
            },
        ]
    }, {
        path: "/login",
        name: "Login",
        meta: {
            title: '用户登录'
        },
        component: () => import ( /* webpackChunkName: "login" */ "../views/Login.vue")
    }, {
        path: "/insidelogin",
        name: "insidelogin",
        meta: {
            title: '内部登录'
        },
        component: () => import ( /* webpackChunkName: "login" */ "../views/InSideLogin.vue")
    },{
        path: "/RLogin",
        name: "RLogin",
        meta: {
            title: '前台登录'
        },
        component: () => import ( /* webpackChunkName: "login" */ "../views/RLogin.vue")
    },{
        path: "/MLogin",
        name: "MLogin",
        meta: {
            title: '内部登录'
        },
        component: () => import ( /* webpackChunkName: "login" */ "../views/MLogin.vue")
    },{
        path: "/register",
        name: "Register",
        meta: {
            title: '注册'
        },
        component: () => import ( /* webpackChunkName: "login" */ "../views/Register.vue")
    }
    , {
        path: "/homepage",
        name: "Homepage",
        meta: {
            title: '首页'
        },
        component: () => import ( /* webpackChunkName: "login" */ "../views/Homepage.vue")
    }
];

const router = createRouter({
    history: createWebHashHistory(),
    routes
});

router.beforeEach((to, from, next) => {
    document.title = `${to.meta.title} | hospital`;
    const role = localStorage.getItem('id');

    if (!role && to.path !== '/login' && to.path !== '/register' && to.path !== '/homepage' && to.path !== '/insidelogin' && to.path !== '/RLogin' && to.path !== '/MLogin') {
        next('/login');
    } else if (to.meta.permission) {
        // 如果是管理员权限则可进入，这里只是简单的模拟管理员权限而已
        role === 'admin'
            ? next()
            : next('/403');
    } else {
        next();
    }
});

export default router;